package com.hsmpay.mobile.service.system.impl;

import com.hsmpay.common.service.impl.BaseServiceSupport;
import com.hsmpay.common.util.DateConvertUtil;
import com.hsmpay.mobile.dao.system.HolidaysDao;
import com.hsmpay.mobile.pojo.system.Holidays;
import com.hsmpay.mobile.service.system.HolidaysService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;

/**
 * 节假日服务接口
 * 对应dao holidaysDao
 *
 * @author 颜铃璋
 *         创建时间：2014-05-05
 */
@Service("holidaysService")
public class HolidaysServiceImpl extends BaseServiceSupport<Holidays, Long> implements HolidaysService<Holidays, Long> {
    @Resource(name = "holidaysDao")
    private HolidaysDao<Holidays, Long> holidaysDao;

    /**
     * 查询该日期是否是 节假日
     *
     * @param date
     * @return
     * @throws Exception
     */
    public boolean isHolidays(Date date) throws Exception {
        boolean festival = false;
        String dateStr = DateConvertUtil.dateString(date);
        Holidays holidays = holidaysDao.searchHolidays(dateStr, 1);//节假日
        if (null != holidays) {//是节日
            festival = true;
        }/*else{//不是节日  判断是否是周六日
            festival = isWeekend(date);
		}*/
        return festival;
    }

    /**
     * 判断当天是不是节假日
     * @throws Exception
     */
    public boolean isRealHolidays(Date date) throws Exception {
        boolean holiday = false;
        Boolean isHoloday = isHolidays(date);
        Boolean isWeekend = isWeekend(date);
        Boolean isWorkDay = isWorkDay(date);
        if(isHoloday){
            holiday = true;
        }else {
            //周六周日判断
            if(isWeekend){
                //查询该日期是否是 工作日
                if(isWorkDay){
                    holiday = false;
                }else {
                    holiday = true;
                }
            }else {
                holiday = false;
            }
        }
        return holiday;
    }

    /**
     * 查询该日期是否是 法定工作日
     *
     * @param date
     * @return
     * @throws Exception
     */
    public boolean isLegalWorkDay(Date date) throws Exception {
        boolean workDay = false;
        String dateStr = DateConvertUtil.dateString(date);
        Holidays holidays = holidaysDao.searchHolidays(dateStr, 2);//法定上班日
        if (null != holidays) {//是法定上班日
            workDay = true;
        }
        return workDay;
    }

    /**
     * 周六周日判断
     *
     * @param date
     * @return
     */
    public boolean isWeekend(Date date) {
        boolean weekend = false;
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
            weekend = true;
        }
        return weekend;
    }

    /**
     * 查询该日期是否是 工作日
     *
     * @param date
     * @return
     * @throws Exception
     */
    public boolean isWorkDay(Date date) throws Exception {
        boolean workDay = false;
        if (isHolidays(date)) {//如果是节假日
            return workDay;
        }

        if (isWeekend(date)) {//如果是 周六日
            if (isLegalWorkDay(date)) {//判断是否是 法定工作日
                workDay = true;
            }//否则 就不修改  默认值  即不是 工作日
        } else {
            workDay = true;
        }
        return workDay;
    }
}